﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ConnectFortyFivedegreeElbow : HYExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)3);
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute(ref string TranName)
		{
			TranName = "45度弯头连接";
			ISelectionFilter selectionFilter = new HoriPipeFilter();
			Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择要用两个45度弯头连接的管道一");
			Element element = base.Doc.GetElement(reference);
			XYZ globalPoint = reference.GlobalPoint;
			Reference reference2 = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择要用两个45度弯头连接的管道二");
			Element element2 = base.Doc.GetElement(reference2);
			XYZ globalPoint2 = reference2.GlobalPoint;
			Element element3 = element as Pipe;
			Pipe pipe = element2 as Pipe;
			YJKLine hyline = new YJKLine((element3.Location as LocationCurve).Curve as Line);
			YJKLine hyline2 = new YJKLine((pipe.Location as LocationCurve).Curve as Line);
			if (Math.Abs(hyline.Line.Direction.AngleTo(hyline2.Line.Direction) / Math.PI * 180.0 - 90.0) < 10.0)
			{
				new YJKPipeConnectorHelper().connect45Elbow(element as Pipe, globalPoint, element2 as Pipe, globalPoint2, base.Doc);
			}
			else
			{
				YJKMessageBox.Show("双45度弯头连接的两根管道角度应接近于90度！");
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
